package com.webdev.servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webdev.domain.Request;
import com.webdev.service.RequestService;
import com.webdev.service.imp.RequestServiceImp;

/**
 * Servlet implementation class UpdateRequestServlet
 */
@WebServlet("/UpdateRequestServlet")
public class UpdateRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * 处理员工更改自己的请假单和老板审核员工的请假单的逻辑
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if(command.equals("approve")) {
			RequestService service = new RequestServiceImp();
			Request req = new Request();
			req = service.getRequestByRid(Integer.parseInt(request.getParameter("rid")));
			req.setRstate("已批准");
			service.updateRequest(req);
		}
		else if(command.equals("refuse")){
			RequestService service = new RequestServiceImp();
			Request req = new Request();
			req = service.getRequestByRid(Integer.parseInt(request.getParameter("rid")));
			req.setRstate("已拒绝");
			service.updateRequest(req);
		}
		else if(command.equals("update")) {
			RequestService service = new RequestServiceImp();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Request req = new Request();
			try {
				req.setRsdate(new Date(sdf.parse(request.getParameter("rsdate")).getTime()));
				req.setRedate(new Date(sdf.parse(request.getParameter("redate")).getTime()));
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			req.setRtype(request.getParameter("rtype"));
			if(!request.getParameter("rinfo").equals("[object HTMLInputElement]")) {
			    req.setRinfo(request.getParameter("rinfo"));
			}
			else {
				req.setRinfo("无");
			}
			req.setPid(Integer.parseInt(request.getParameter("pid")));
			req.setRstate("待审核");
			service.updateRequest(req);
		}
	}

}
